package com.newsanalysis.repository;

import com.newsanalysis.model.entity.News;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface NewsRepository extends JpaRepository<News, Long> {
    
    Page<News> findByCategory(String category, Pageable pageable);
    
    Page<News> findByTitleContainingIgnoreCase(String title, Pageable pageable);
    
    @Query("SELECT n FROM News n WHERE n.title LIKE %:keyword% OR n.content LIKE %:keyword%")
    Page<News> searchByKeyword(@Param("keyword") String keyword, Pageable pageable);
    
    List<News> findTop10ByOrderByHotIndexDesc();
    
    @Query("SELECT n.category, COUNT(n) FROM News n GROUP BY n.category")
    List<Object[]> countByCategory();
    
    @Query("SELECT n.sentiment, COUNT(n) FROM News n GROUP BY n.sentiment")
    List<Object[]> countBySentiment();
}

